﻿Public Class FTBagiSiswa

    'member variables
    Private _cSemester As CSemester
    Private _semesterAktif As DataTable
    Private _cSiswa As CSiswa
    Private _dtabSiswaBaru As DataTable
    Private _cKelas As CKelas
    Private _listSiswaKelas As List(Of DataTable)
    Private _cPembagianKelas As CPembagianKelas
    Private _colIdKelas As New List(Of Integer)
    Private _colDtab As New List(Of DataTable)
    Private _jmlSelectedSiswaBaruLaki As Integer
    Private _jmlSelectedSiswaBaruPerempuan As Integer
    ''' <summary>
    ''' state dari form
    ''' 0 = normal/awal
    ''' 1 = bagi per kelas
    ''' 2 = bagi acak
    ''' </summary>
    ''' <remarks></remarks>
    Private _formState As Integer

    'forms
    Private WithEvents _flSemester As FLSemester

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        dgSiswaBaru.AutoGenerateColumns = False
        dgSiswaKelas.AutoGenerateColumns = False

        'set data jenis kelamin datagridview
        Dim dtabJKel As New DataTable
        dtabJKel.Columns.Add("nama")
        dtabJKel.Columns.Add("id")
        Dim aRow As DataRow = dtabJKel.NewRow
        aRow.Item("nama") = "laki-laki"
        aRow.Item("id") = "L"
        dtabJKel.Rows.Add(aRow)
        aRow = dtabJKel.NewRow
        aRow.Item("nama") = "Perempuan"
        aRow.Item("id") = "P"
        dtabJKel.Rows.Add(aRow)
        With cjkel
            .DataSource = dtabJKel
            .DisplayMember = "nama"
            .ValueMember = "id"
        End With


        Dim dtabjkel2 As New DataTable
        dtabjkel2 = dtabJKel.Copy
        With CjKel2
            .DataSource = dtabjkel2
            .DisplayMember = "nama"
            .ValueMember = "id"
        End With

        _jmlSelectedSiswaBaruLaki = 0
        _jmlSelectedSiswaBaruPerempuan = 0

        _formState = 0
    End Sub

#Region "SettinganAwal"
    '-----------------------------------------------------------------------------------------------
    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles LabelJudul.MouseDown, PanelJudul.MouseDown, MyBase.MouseDown

        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = True
            MouseDownX = e.X
            MouseDownY = e.Y
        End If
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseUp, LabelJudul.MouseUp, PanelJudul.MouseUp

        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = False
        End If
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove, LabelJudul.MouseMove, PanelJudul.MouseMove

        If IsFormBeingDragged Then
            Dim temp As Point = New Point()

            temp.X = Me.Location.X + (e.X - MouseDownX)
            temp.Y = Me.Location.Y + (e.Y - MouseDownY)
            Me.Location = temp
            temp = Nothing
        End If
    End Sub


    Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PanelJudul.Paint
        Dim p As New Pen(Color.FromArgb(213, 251, 185), 2)
        'set text box border
        PaintTextBox(Me, e.Graphics, p)

        p.Dispose()
    End Sub

    Private Sub PaintTextBox(ByVal cont As Control, ByVal g As Graphics, ByVal p As Pen)
        For Each ctl As Control In cont.Controls
            If TypeOf ctl Is TextBox Then
                g.DrawRectangle(p, New Rectangle(ctl.Location + New Size(1, 1), ctl.Size - New Size(2, 2)))
            ElseIf TypeOf ctl Is Panel Then
                PaintTextBox(ctl, g, p)
            ElseIf TypeOf ctl Is GroupBox Then
                PaintTextBox(ctl, g, p)
            End If
        Next
    End Sub

    Private Sub SetRegionTextBox(ByVal cont As Control)
        For Each ctl As Control In cont.Controls
            If TypeOf ctl Is TextBox Then
                If ctl.Region Is Nothing Then
                    ctl.Region = New Region(New Rectangle(2, 2, ctl.Width - 4, ctl.Height - 4))
                Else
                    ctl.Region = Nothing
                End If
            ElseIf TypeOf ctl Is Panel Then
                SetRegionTextBox(ctl)
            ElseIf TypeOf ctl Is GroupBox Then
                SetRegionTextBox(ctl)
            End If
        Next
    End Sub

    Private Sub btnMaximize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMaximize.Click

        WindowState = FormWindowState.Maximized
        btnMinimize.Visible = False
        btnMaximize.Visible = False
        btnClose.Visible = False
    End Sub

    Private Sub btnMinimize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinimize.Click
        WindowState = FormWindowState.Minimized
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub FSizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
        If WindowState <> FormWindowState.Maximized Then
            btnMinimize.Visible = True
            btnMaximize.Visible = True
            btnClose.Visible = True
        End If
    End Sub
    '-----------------------------------------------------------------------------------------------
#End Region

    Private Sub FTBagiSiswa_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'set text box border
        SetRegionTextBox(Me)

        'split sama besar
        Me.SplitContainer1.SplitterDistance = (SplitContainer1.Width - SplitContainer1.SplitterWidth) / 2

        'set semester aktif
        _cSemester = New CSemester
        _semesterAktif = _cSemester.getSemesterAktif()
        tbSemester.Text = _semesterAktif(0).Item("nama")
        tbIdSemester.Text = _semesterAktif(0).Item("id")
    End Sub

    Private Sub tbSemester_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles tbSemester.Enter, tbSemester.Click
        'form cover
        Dim fCov As New FCover
        fCov.Show()

        'menampilkan form list semester
        _flSemester = New FLSemester
        _flSemester.ShowDialog()

        fCov.Close()
    End Sub

    Private Sub _flSemester_DataTerpilih() Handles _flSemester.DataTerpilih
        'set semester yang di pilih
        tbSemester.Text = _flSemester.SelectedNamaSemester
        tbIdSemester.Text = _flSemester.SelectedIDSemester
    End Sub

    Private Sub setState(stateInteger As Integer)
        Select Case stateInteger
            Case 0
                SplitContainer1.Panel1.Enabled = False
                SplitContainer1.Panel2.Enabled = False
                btnReset.Enabled = False
                btnSimpan.Enabled = False
                rbPerKelas.Enabled = True
                rbAcak.Enabled = True
                btnTampil.Enabled = True
                btnAcak.Visible = False
                rbPerKelas.Checked = True
                'reset data
                Try
                    _dtabSiswaBaru.Reset()


                    _colIdKelas.Clear()
                    _colDtab.Clear()
                    _jmlSelectedSiswaBaruLaki = 0
                    _jmlSelectedSiswaBaruPerempuan = 0
                    dgSiswaBaru.DataSource = Nothing
                    dgSiswaKelas.DataSource = Nothing
                    cbKelas.DataSource = Nothing
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            Case 1
                SplitContainer1.Panel1.Enabled = True
                SplitContainer1.Panel2.Enabled = True
                btnReset.Enabled = True
                btnSimpan.Enabled = True
                rbPerKelas.Enabled = False
                rbAcak.Enabled = False
                btnTampil.Enabled = False
            Case 2
                SplitContainer1.Panel1.Enabled = False
                SplitContainer1.Panel2.Enabled = True
                btnReset.Enabled = True
                btnSimpan.Enabled = True
                rbPerKelas.Enabled = False
                rbAcak.Enabled = False
        End Select

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnTampil.Click
        setState(1)

        'tampilkan daftar siswa baru
        _cSiswa = New CSiswa
        _dtabSiswaBaru = _cSiswa.getSiswaBaru
        dgSiswaBaru.DataSource = _dtabSiswaBaru

        'set kelas
        _cKelas = New CKelas
        _cPembagianKelas = New CPembagianKelas
        cbKelas.DataSource = _cKelas.getByTingakatan(1)
        cbKelas.DisplayMember = "nama"
        cbKelas.ValueMember = "id"


    End Sub

    Private Sub dgSiswaBaru_CellContentClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgSiswaBaru.CellContentClick
        'set keterangan dgSiswabaru
        If dgSiswaBaru.Rows(e.RowIndex).Cells(e.ColumnIndex).GetType = GetType(DataGridViewCheckBoxCell) Then
            If dgSiswaBaru.Rows(e.RowIndex).Cells(e.ColumnIndex).EditedFormattedValue Then
                If dgSiswaBaru.Rows(e.RowIndex).Cells("cjkel").Value = "L" Then
                    _jmlSelectedSiswaBaruLaki += 1
                ElseIf dgSiswaBaru.Rows(e.RowIndex).Cells("cjkel").Value = "P" Then
                    _jmlSelectedSiswaBaruPerempuan += 1
                End If
            Else
                If dgSiswaBaru.Rows(e.RowIndex).Cells("cjkel").Value = "L" Then
                    _jmlSelectedSiswaBaruLaki -= 1
                ElseIf dgSiswaBaru.Rows(e.RowIndex).Cells("cjkel").Value = "P" Then
                    _jmlSelectedSiswaBaruPerempuan -= 1
                End If
            End If

            lblKeteranganSIswaBaru.Text = "Terpilih : " & (_jmlSelectedSiswaBaruLaki + _jmlSelectedSiswaBaruPerempuan) & "     Laki-laki : " & _jmlSelectedSiswaBaruLaki & "    Perempuan : " & _jmlSelectedSiswaBaruPerempuan
        End If
    End Sub


    Private Sub dgSiswaBaru_RowPostPaint(sender As Object, e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles dgSiswaBaru.RowPostPaint, dgSiswaKelas.RowPostPaint
        'menampilkan nomor urut pada datagridview

        If sender Is dgSiswaBaru Then
            sender.Rows(e.RowIndex).Cells("CNo").Value = e.RowIndex + 1

            'dgSiswaBaru.Rows(e.RowIndex).Cells("CJenisKelamin").Value = IIf(dgSiswaBaru.Rows(e.RowIndex).Cells("Cjk").Value = "P", "Perempuan", "Laki-laki")

        ElseIf sender Is dgSiswaKelas Then
            sender.Rows(e.RowIndex).Cells("CNo2").Value = e.RowIndex + 1

            'dgSiswaKelas.Rows(e.RowIndex).Cells("cjeniskelamin2").Value = IIf(dgSiswaKelas.Rows(e.RowIndex).Cells("Cjk2").Value = "P", "Perempuan", "Laki-laki")

        End If
    End Sub

    Private Sub rbPerKelas_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbPerKelas.CheckedChanged, rbAcak.CheckedChanged
        If rbPerKelas.Checked Then
            btnTampil.Visible = True
            btnAcak.Visible = False
        Else
            btnTampil.Visible = False
            btnAcak.Visible = True
        End If

    End Sub

    Private Sub btnBagi_Click(sender As System.Object, e As System.EventArgs) Handles btnBagi.Click
     
        If _colIdKelas.Contains(cbKelas.SelectedValue) Then
            Dim dtab As DataTable = _colDtab.Item(_colIdKelas.IndexOf(cbKelas.SelectedValue))

            For Each dgrow As DataGridViewRow In dgSiswaBaru.Rows
                If CType(dgrow.Cells(CPilih.Index), DataGridViewCheckBoxCell).Value = True Then
                    Dim aRow As DataRow = dtab.NewRow
                    aRow.Item("id") = dgrow.Cells("cid").Value
                    aRow.Item("nama") = dgrow.Cells("cnama").Value
                    aRow.Item("jk") = dgrow.Cells("cjkel").Value
                    dtab.Rows.Add(aRow)
                End If
            Next

            _colDtab.Item(_colIdKelas.IndexOf(cbKelas.SelectedValue)) = dtab

            dgSiswaKelas.DataSource = dtab
        Else
            Dim dtab As New DataTable
            dtab.Columns.Add("id")
            dtab.Columns.Add("nama")
            dtab.Columns.Add("jk")

            For Each dgrow As DataGridViewRow In dgSiswaBaru.Rows
                If CType(dgrow.Cells(CPilih.Index), DataGridViewCheckBoxCell).Value = True Then
                    Dim aRow As DataRow = dtab.NewRow
                    aRow.Item("id") = dgrow.Cells("cid").Value
                    aRow.Item("nama") = dgrow.Cells("cnama").Value
                    aRow.Item("jk") = dgrow.Cells("cjkel").Value
                    dtab.Rows.Add(aRow)
                End If
            Next

            dgSiswaKelas.DataSource = dtab

            _colIdKelas.Add(cbKelas.SelectedValue)
            _colDtab.Add(dtab)
        End If

        'hapus data yang di pindah
        For Each kRow As DataGridViewRow In dgSiswaKelas.Rows
            For Each bRow As DataGridViewRow In dgSiswaBaru.Rows
                If bRow.Cells("cid").Value = kRow.Cells("cid2").Value Then
                    dgSiswaBaru.Rows.Remove(bRow)
                    Exit For
                End If
            Next
        Next
        lblKeteranganSIswaBaru.Text = ""
    End Sub

    Private Sub btnKembali_Click(sender As System.Object, e As System.EventArgs) Handles btnKembali.Click

    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles btnKeluar.Click
        Me.Close()
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles btnReset.Click
        setState(0)

    End Sub

    Private Sub cbKelas_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cbKelas.SelectedIndexChanged
        'buka data kelas
        Try
            dgSiswaKelas.DataSource = _colDtab.Item(_colIdKelas.IndexOf(cbKelas.SelectedValue))
        Catch ex As Exception
            If ex.Message.Contains("out of range") Then
                dgSiswaKelas.DataSource = Nothing
            ElseIf ex.Message.Contains("DataRowView") Then

            Else
                MsgBox(ex.Message)
            End If

        End Try



    End Sub

    Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        For Each aRow As DataGridViewRow In dgSiswaBaru.Rows
            aRow.Cells("cpilih").Value = True
        Next
    End Sub

    Private Sub Button2_Click_1(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        For Each aRow As DataGridViewRow In dgSiswaBaru.Rows
            aRow.Cells("cpilih").Value = False
        Next
    End Sub

    Private Sub btnSimpan_Click(sender As System.Object, e As System.EventArgs) Handles btnSimpan.Click
        'simpan data
        

        _cPembagianKelas = New CPembagianKelas

        For Each anId As Integer In _colIdKelas
            Dim dTab As New DataTable
            dTab.Columns.Add("id_smstr")
            dTab.Columns.Add("id_kls")
            dTab.Columns.Add("id_ssw")

            Dim kTab As DataTable = _colDtab.Item(_colIdKelas.IndexOf(anId))
            For Each aTab As DataRow In kTab.Rows
                Dim aRow As DataRow = dTab.NewRow
                aRow.Item("id_kls") = anId
                aRow.Item("id_smstr") = tbIdSemester.Text
                aRow.Item("id_ssw") = aTab.Item("id")
                dTab.Rows.Add(aRow)
            Next
            _cPembagianKelas.simpan(dTab)
        Next
        MsgBox("ok selesai")
    End Sub
End Class